<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import { addPool } from '#/api/gis/valve';
import type { AddPoolParams } from '#/api/gis/valve';
import { message } from 'ant-design-vue';
import { z } from '#/adapter/form';

const [AddModal, AddmodalApi] = useVbenModal({
});

const [BaseForm] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入阀池编号',
      },
      fieldName: 'poolCode',
      label: '阀池编号',
      rules:"required",
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入阀池名称',
      },
      fieldName: 'poolName',
      label: '阀池名称',
      rules:"required",
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入关联管线编号',
      },
      fieldName: 'pipelineCode',
      label: '关联管线编号',
      rules:"required",
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
        {
            label: '蝶型',
            value: 2,
          },
          {
            label: '球型',
            value: 1,
          },
          {
            label: '闸型',
            value: 0,
          },
        ],
        placeholder: '请选择阀池类型',
      },
      fieldName: 'valveType',
      label: '阀池类型',
      rules:"selectRequired",
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入经度',
      },
      fieldName: 'longitude',
      label: '经度',
      rules: z.string()
        .min(1, { message: '请输入经度' })
        .refine((value) => {
          const numValue = parseFloat(value);
          return !isNaN(numValue) && numValue >= -180 && numValue <= 180;
        }, {
          message: '经度必须在 -180 到 180 之间',
        }),
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入纬度',
      },
      fieldName: 'latitude',
      label: '纬度',
      rules: z.string()
        .min(1, { message: '请输入纬度' })
        .refine((value) => {
          const numValue = parseFloat(value);
          return !isNaN(numValue) && numValue >= -90 && numValue <= 90;
        }, {
          message: '纬度必须在 -90 到 90 之间',
        }),
    },
    
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
      },
      fieldName: 'remark',
      label: '备注',
    },
  ],
});

// 定义一个自定义事件
const emit = defineEmits(['refreshTable']);
function onSubmit(values: Record<string, any>) {
  const params = values as AddPoolParams;
  params.location = `POINT(${params.longitude} ${params.latitude})`;
  addPool(params).then(() => {
    AddmodalApi.close();
    message.success('添加阀池成功');
    emit('refreshTable'); // 触发自定义事件
  }).catch((error) => {
    console.error('添加阀池失败:', error);
   // message.error('添加阀池失败');
  });
}
</script>

<template>
  <AddModal title="添加油水阀池">
    <BaseForm />
    <template #prepend-footer>
    </template>
  </AddModal>
</template>
